package ro.qi.framework.sys.db;

/**
 * @author Denverash
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import ro.qi.framework.sys.debug.Logger;

public class ConnectionManager {

	// connection to database
	private boolean started = false;
	private DataSource jdbcURL = null;
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private String datasourceName = null;
	public static InitialContext initialContext = null;
	public static Context envContext = null;

	public ConnectionManager() {
	}

	public ConnectionManager(String name) {
		setResourceName(name);
	}

	public void setResourceName(String name) {
		datasourceName = name;
		start();
	}

//	public Connection getConnection(Context envContext, String datasourceName) {
//		try {
////			Logger.error(this, "QA11");
//			jdbcURL = (DataSource) envContext.lookup("java:comp/env/" + datasourceName);
//
////			Logger.error(this, "QA12");
//			conn = jdbcURL.getConnection();
//			return conn;
//		} catch (NamingException nexc) {
//			started = false;
////			Logger.error(this, "QA1");
//			Logger.error(this, datasourceName + " name not found... " + nexc.getMessage() + " " + nexc.getExplanation());
//		} catch (SQLException sexc) {
//			started = false;
//			Logger.error(this, "Couldn't make the connection...");
//		}
//		return null;
//	}

	public boolean start() {
		try {
			
			if (initialContext == null) {
				initialContext = new InitialContext();
			}

//			if (envContext == null) {
//				envContext = (Context)initialContext.lookup("java:comp/env/");
//			}

//			Hashtable env = new Hashtable();
//			env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.Websphere.naming.WsnInitialContextFactory");
//			InitialContext ctx = new InitialContext(env);

			jdbcURL = (DataSource) initialContext.lookup("java:comp/env/" + datasourceName);

//			jdbcURL = (DataSource) envContext.lookup(datasourceName);

			conn = jdbcURL.getConnection();
			started = true;

			return true;
		} catch (NamingException nexc) {
			initialContext = null;
			envContext = null;
			started = false;
//			Logger.error(this, "QA2");
			Logger.error(this, datasourceName + " name not found... " + nexc.getMessage() + " " + nexc.getExplanation());
		} catch (SQLException sexc) {
			started = false;
			Logger.error(this, "Couldn't make the connection...");
		}
		return false;
	}

	public void close() {
		try {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		} catch (SQLException sexc) {
			// do nothing
		}
	}

	public Connection getConnection() throws SQLException {
		if (!started) {
			if (!start()) {
				throw new SQLException("Connection not initialized, please check the datasource settings.");
			}
		}
		return conn;
	}

}
